package com.mijie.homi.search.util;

import java.io.File;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;


/**
 * @author chongxin
 * @since 2013/6/19
 * @version Lucene 4.3.1
 * */
public class LuceneSearch {
	// 声明一个IndexSearcher对象
	private IndexSearcher searcher = null;
	// 声明一个Query对象
	private Query query = null;
	private String field = "contents";
	public Set   lookSet=new HashSet<Integer>();

	public LuceneSearch() {
		try {
			IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(Constants.INDEX_STORE_PATH)));
		
			searcher = new IndexSearcher(reader);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    //返回查询结果 
	public final TopDocs search(String keyword) {
		System.out.println("正在检索关键字 : " + keyword);
		try {
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
			QueryParser parser = new QueryParser(Version.LUCENE_40, field,analyzer);
			// 将关键字包装成Query对象
			query = parser.parse(keyword);
			Date start = new Date();
		//	1 根据关键字只取索引库中的前100条document，然后把这100条document按某种方式进行排序，安得分从高到低返回结果。 
			TopDocs results = searcher.search(query, 5 * 2);
			Date end = new Date();
			System.out.println("检索完成，用时" + (end.getTime() - start.getTime())
					+ "毫秒");
			return results;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	 //返回查询结果 
	public final TopDocs search4WhitespaceAnalyzer(String keyword) {
		System.out.println("正在检索关键字 : " + keyword);
		try {
			Analyzer analyzer = new WhitespaceAnalyzer(Version.LUCENE_40);
			QueryParser parser = new QueryParser(Version.LUCENE_40, field,analyzer);
			// 将关键字包装成Query对象
			query = parser.parse(keyword);
			Date start = new Date();
			TopDocs results = searcher.search(query, 5 * 2);
			Date end = new Date();
			System.out.println("检索完成，用时" + (end.getTime() - start.getTime())
					+ "毫秒");
			return results;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	
	
	public String search4tonitsi(String keyword) {
		TopDocs h = null;
//		h = test.search("中国");
//		test.printResult(h);
//		h = test.search("人民");
//		test.printResult(h);
//		h = test.search("共和国");
//		test.printResult(h);
		h=search4WhitespaceAnalyzer(keyword);
		String r=		toString(h,keyword);
		
		//致力,努力,努力,尽力,努力,勉力,高兴,努力,努力,起劲,努力,积极,通力合作,共同努力,重新努力别辟门户,另起炉灶,努目,瞪眼,有力,无力
		
		return r;
	}
      //打印结果
	public void printResult(TopDocs results) {
		ScoreDoc[] h = results.scoreDocs;
		if (h.length == 0) {
			System.out.println("对不起，没有找到您要的结果。");
		} else {
			for (int i = 0; i < h.length; i++) {
				try {
					Document doc = searcher.doc(h[i].doc);
					System.out.print("这是第" + i + "个检索到的结果，为：");
					System.out.println(doc.get("contents"));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println("--------------------------");
	}

	
	public String toString(TopDocs results,String keyword) {
		lookSet.clear();
		if(results==null)return "noresultcaa";
		String s="";
		ScoreDoc[] h = results.scoreDocs;
		if (h.length == 0) {
			System.out.println("对不起，没有找到您要的结果。");
		} else {
			for (int i = 0; i < h.length; i++) {
				try {
					Document doc = searcher.doc(h[i].doc);
					 
					
						String result = doc.get("contents");
						result=result.replaceAll(" ", ",");
						if(result.contains(keyword))
						{
						s=s+","+result;
						
						//ati ca2
						Integer linenum= Integer.parseInt( doc.get("linenum"));
						lookSet.add(linenum);
					
						}
					 
					//System.out.println(doc.get("contents"));
				} catch (Exception e) {
					e.printStackTrace();
					
				}
			}
		}
		s=strUtil.trimx(",", s);
		return s;
		 
	}
	
	
	public static void main(String[] args) throws Exception {
		LuceneSearch test = new LuceneSearch();
		TopDocs h = null;
//		h = test.search("中国");
//		test.printResult(h);
//		h = test.search("人民");
//		test.printResult(h);
//		h = test.search("共和国");
//		test.printResult(h);
		String keyword = "自己";
//		h=test.search(keyword);
//		test.printResult(h);
		
		String s=test.search4tonitsi(keyword);
		System.out.println(s);
		
		
	}

}
 